package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.WmArticlePage;
import com.heima.model.wemedia.dtos.WmDimensionDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmNewsStatistics;
import com.heima.utils.common.DateUtils;
import com.heima.wemedia.mapper.WmStaticMapper;
import com.heima.wemedia.service.WmNewsService;
import com.heima.wemedia.service.WmStaticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: liudehuan
 * @description
 * @date: 2023-04-22 18:06
 */
@Service
public class WmStaticServiceImpl extends ServiceImpl<WmStaticMapper, WmNewsStatistics> implements WmStaticService {

    @Autowired
    private WmNewsService wmNewsService;

    /**
     * 首页加载点赞，收藏数量
     * @param beginDate
     * @param endDate
     * @return
     */
    @Override
    public ResponseResult getNewsDimension(String beginDate, String endDate) {
        WmDimensionDto dto = new WmDimensionDto();
        Date beg = DateUtils.stringToDate(beginDate);
        Date end = DateUtils.stringToDate(endDate);


        LambdaQueryWrapper<WmNewsStatistics> lbw = new LambdaQueryWrapper<>();
        lbw.ge(WmNewsStatistics::getCreatedTime,beg).le(WmNewsStatistics::getCreatedTime,end);

        int forward = 0,likes = 0,collection = 0;
        List<WmNewsStatistics> list = this.list(lbw);
        for (WmNewsStatistics statistics : list) {
            if(null != statistics.getLikes()){
                likes+=statistics.getLikes();
            }
            if(null != statistics.getForward()){
                forward+=statistics.getForward();
            }
            if(null != statistics.getCollection()){
                collection+=statistics.getCollection();
            }
        }
        int push = this.count();

        dto.setPublishNum(push);
        dto.setCollectNum(collection);
        dto.setForwardNum(forward);
        dto.setLikesNum(likes);

        return ResponseResult.okResult(dto);
    }

    /**
     * 加载首页文章数据
     * @param beginDate
     * @param endDate
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResponseResult getNewsPage(String beginDate, String endDate, Integer page, Integer size) {
        Date beg = DateUtils.stringToDate(beginDate);
        Date end = DateUtils.stringToDate(endDate);


        LambdaQueryWrapper<WmNewsStatistics> lbw = new LambdaQueryWrapper<>();
        lbw.ge(WmNewsStatistics::getCreatedTime,beg).le(WmNewsStatistics::getCreatedTime,end);

        Page<WmNewsStatistics> p = new Page<>(page,size);
        Page<WmNewsStatistics> statisticsPage = this.page(p, lbw);

        List<WmNewsStatistics> records = statisticsPage.getRecords();
        List<WmArticlePage> list = new ArrayList<>();
        for (WmNewsStatistics record : records) {
            WmNews wmNews = wmNewsService.getById(record.getArticle());
            if (wmNews.getStatus() != 9){
                continue;
            }

            WmArticlePage dto = new WmArticlePage();
            dto.setId(record.getArticle());
            dto.setTitle(wmNewsService.getTitleById(record.getArticle()));
            dto.setComment(record.getComment());
            dto.setLikes(record.getLikes());
            dto.setCollection(record.getCollection());
            dto.setViews(record.getReadCount());
            list.add(dto);
        }

        ResponseResult result = new ResponseResult();
        result.setCode(200);
        result.setData(list);
        result.setTotal(list.size());
        result.setErrorMessage("操作成功");
        result.setHost("");
        return result;
    }
}
